home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 12.1 KB | 390 lines | [TEXT/MPS ] |
- /*
- File: SampleLibrary.cp
-
- Contains: Implementation of the CCPlusSampleLibrary shared library.
-
- Copyright: © 1993-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- #include <GlobalNew.h>
- #include <LibraryManager.h>
- #include <LibraryManagerClasses.h>
- #include <LibraryManagerUtilities.h>
-
- #include <String.h>
- #include <Events.h>
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <StdIO.h>
-
- #include "SampleLibrary.h"
-
- /*————————————————————————————————————————————————————————————————————————————————————
- class TTrafficLight
-
- Since we aren't exporting this class (we could if we wanted to), we put its
- interface here rather than in SampleLibrary.h, which clients of the library
- get to see.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- class TTrafficLight : public TDynamic {
- public:
- TTrafficLight();
- virtual ~TTrafficLight();
-
- // public virtual methods
-
- virtual Boolean IsValid() const; // Override
-
- virtual Boolean GetLightState() const; // returns the state of the light
- virtual void SetLightState( Boolean state ); // set it to new state
- virtual void DrawLight(); // displays the light
- virtual void AdjustMenus( Boolean update );
- virtual void DoMenuCommand( short menuItem );
-
- protected:
- static Boolean GoGetRect( short rectID, Rect *theRect );
-
- private:
- Boolean fLightState; // 0:indicates OFF, 1:indicates ON
- Rect fStopRect; // rectangle for stop light
- Rect fGoRect; // rectangle for go rectangle
- WindowPtr fWindow; // objects grafport and window
- };
-
-
- //————————————————————————————————————————————————————————————————————————————————————
- // TTrafficLight
- //
- // TrafficLight constructor, allocates window record from client's pool, and ini-
- // tialize our light rectangles.
- //————————————————————————————————————————————————————————————————————————————————————
-
- TTrafficLight::TTrafficLight()
- {
- long savedrefnum = -1;
- Ptr wstorage = nil;
-
- VOLATILE(wstorage); // use this if variable is being modified within
- VOLATILE(savedrefnum); // TRY and accessed in the CATCH_ALL.
-
- fWindow = nil;
-
- Trace("TTrafficLight Constructor\n"); // send the output to trace monitor's window
-
- TRY
- fLightState = false;
-
- wstorage = (Ptr)new WindowRecord;// allocate our storage
-
- FailNULL( wstorage, ErrorCode(), "sorry not enought memory in pool");
-
- // include the library's file in resource chain to obtain our menus & windows
-
- Fail(GetLocalLibraryFile()->Preflight(savedrefnum), "Failed to preflight");
-
- fWindow = GetNewWindow( rWindow, wstorage, (WindowPtr) -1 );
-
- FailNULL( fWindow, ResError(), "sorry unable to create a window");
-
- TTrafficLight::GoGetRect(rStopRect, &fStopRect); // get rectangle for Stop light
- TTrafficLight::GoGetRect(rGoRect, &fGoRect); // get rectangle for Go light
-
- // Any MENU resources in our shared library is appended to our application
-
- MenuHandle themenu = GetMenu( mLight ); // get our traffic light menu
- InsertMenu( themenu, 0 ); // append to the application's menu
-
- DrawMenuBar(); // go ahead and update the menu bar
- CATCH_ALL // oh no! something failed lets cleanup
- delete wstorage; // remove the storage
- delete fWindow; // free the pool
- ENDTRY
-
- if (savedrefnum != -1)
- GetLocalLibraryFile()->Postflight(savedrefnum); // restore the resource chain
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // ~TTrafficLight
- //
- // our traffic light destructor, delete the window, and unregister the object for
- // inspector
- //————————————————————————————————————————————————————————————————————————————————————
-
- TTrafficLight::~TTrafficLight()
- {
- Trace("TTrafficLight Destructor\n");// tell Trace Monitor where we are
-
- CloseWindow( fWindow ); // close the window
- delete fWindow; // we are done, release the memory
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // IsValid
- //
- // returns true if the object was initialized properly after it was created. In our
- // case if window has been allocated, we return true.
- //————————————————————————————————————————————————————————————————————————————————————
-
- Boolean TTrafficLight::IsValid() const
- {
- return( fWindow != nil ); // return true if window has been allocated
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // GetLightState
- //
- // return current state of traffic light
- //————————————————————————————————————————————————————————————————————————————————————
-
- Boolean TTrafficLight::GetLightState() const
- {
- Trace("TTrafficLight::GetLightState\n");// tell Trace Monitor where we are
- return fLightState; // return the state of Traffic Light
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // SetLightState
- //
- // set the light to the new state
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::SetLightState( Boolean newState )
- {
- Trace("TTrafficLight::SetLightState\n");// tell Trace Monitor what we are doing
- fLightState = newState; // set the new state
-
- SetPort(fWindow); // make sure we are at the right port before
- InvalRect(&fWindow->portRect); // invalidating the update region
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // DrawLight
- //
- // draw the actual traffic light
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::DrawLight()
- {
- GrafPtr oldport;
-
- Trace("TTrafficLight::DrawLight\n");// tell the Trace Monitor what we are doing
-
- if( fWindow ) {
-
- GetPort( &oldport ); // save the old port
- SetPort( fWindow ); // set the new port to our object's port
-
- EraseRect(&fWindow->portRect); // clear out any garbage that may linger
- if ( fLightState ) // draw a red (or white) stop light
- ForeColor(redColor);
- else
- ForeColor(whiteColor);
-
- PenSize( 2, 2 );
- PaintOval(&fStopRect);
- ForeColor(blackColor);
- FrameOval(&fStopRect);
- if ( ! fLightState ) // draw a green (or white) go light
- ForeColor(greenColor);
- else
- ForeColor(whiteColor);
- PaintOval(&fGoRect);
- ForeColor(blackColor);
- FrameOval(&fGoRect);
- PenSize( 1, 1 );
-
- SetPort( oldport ); // restore the grafpointer
- }
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // AdjustMenus
- //
- // Enable and disable traffic light's menus based on the current state.
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::AdjustMenus( Boolean active )
- {
- long savedrefnum;
- MenuHandle menu;
- OSErr err;
-
- Trace("TTrafficLight::AdjustMenus\n");// tell the Trace Monitor where we are
-
- // include the library's file in resource chain
- if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
- Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
- return;
- }
-
- if( menu = GetMenuHandle(mLight) ) { // get the handle to traffic light menu
-
- if ( active ) { // do we need to enable them?
- EnableItem(menu, iStop);
- EnableItem(menu, iGo);
- } else {
- DisableItem(menu, iStop);
- DisableItem(menu, iGo);
- }
-
- CheckItem(menu, iStop, fLightState); // we can also determine check/uncheck state, too
- CheckItem(menu, iGo, ! fLightState);
- }
- else
- Trace("GetMenuHandle returned NIL\n"); // let Trace Monitor know we failed
- // restore the resource change
- err = GetLocalLibraryFile()->Postflight(savedrefnum);
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // DoMenuCommand
- //
- // Enable and disable menus based on the current state of traffic light.
- //————————————————————————————————————————————————————————————————————————————————————
-
- void TTrafficLight::DoMenuCommand( short menuItem )
- {
- long savedrefnum;
- OSErr err;
-
- Trace("TTrafficLight::DoMenuCommand\n");// tell Trace Monitor what we are doing
- // include the library's file in resource chain
- if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
- Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
- return;
- }
-
- switch ( menuItem ) { // what menu item selected?
- case iStop:
- SetLightState( true ); // set and update
- break;
- case iGo:
- SetLightState( false ); // set and update
- break;
- }
- // restore the resource change
- err = GetLocalLibraryFile()->Postflight(savedrefnum);
- }
-
- //————————————————————————————————————————————————————————————————————————————————————
- // GoGetRect
- //
- // loads the global rectrangles that are used for drawing the traffic lights.
- //————————————————————————————————————————————————————————————————————————————————————
-
- Boolean TTrafficLight::GoGetRect( short rectID, Rect *theRect)
- {
- Handle resource;
-
- TRY
- resource = GetResource('RECT', rectID); // get 'RECT' resource from library file
- FailNULL( resource, ResError(), "'RECT' resource is missing" );
- *theRect = **((Rect**) resource);
- CATCH_ALL
- DebugStr("\pFailed to get 'RECT' resource");// inform the user we failed
- SetRect( theRect, 10, 10, 60, 60 );
- return false;
- ENDTRY
-
- return true;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- NewTrafficLight
-
- creates an instance of our traffic light, and return the pointer to the object.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- TTrafficLight *NewTrafficLight()
- {
- TTrafficLight *trafficlight;
-
- TRY
- trafficlight = new TTrafficLight;// allocate space from default pool
- FailNULL( trafficlight, ErrorCode(), "Failed to create a TTrafficLight object");
- if( ! trafficlight->IsValid() ) {// if we unable to initialize it
- Trace("Failed to initialize TTrafficLight");
- delete trafficlight; // failed, so deallocate the object
- trafficlight = nil; // return nil to inform the client
- }
- CATCH_ALL
- Trace("Failed to create an instance of TTrafficLight");
- ENDTRY
-
- return trafficlight;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- FreeTrafficLight
-
- delete the traffic light object created by NewTrafficLight
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void FreeTrafficLight( TTrafficLight *trafficLight )
- {
- delete trafficLight;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- SetLightState
-
- Given a pointer to trafficlight object, set the traffic light to new state.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void SetLightState( TTrafficLight *trafficLight, Boolean state )
- {
- trafficLight->SetLightState( state );
-
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- GetLightState
-
- Given a pointer to trafficlight object, get the state of the traffic light.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- Boolean GetLightState( TTrafficLight *trafficLight )
- {
- return trafficLight->GetLightState();
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- DrawLight
-
- Given a pointer to trafficlight object, draw the traffic light.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void DrawLight( TTrafficLight *trafficLight )
- {
- trafficLight->DrawLight();
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- AdjustTrafficLightMenus
-
- Given a pointer to trafficlight object, adjust the menus associated with traffic
- light.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void AdjustTrafficLightMenus( TTrafficLight *trafficLight, Boolean active )
- {
- trafficLight->AdjustMenus( active );
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- DoTrafficLightMenuCommand
-
- Given a pointer to trafficlight object, process menu commands associated with our
- traffic light.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- void DoTrafficLightMenuCommand( TTrafficLight *trafficLight, short menuItem )
- {
- trafficLight->DoMenuCommand( menuItem );
- }